﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;

namespace the_seven_1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent(); // 这里是自动生成的初始化方法
            PopulateTreeView(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
        }

        private void PopulateTreeView(string rootPath)
        {
            DirectoryInfo rootDirectory = new DirectoryInfo(rootPath);
            foreach (var item in rootDirectory.GetDirectories())
            {
                TreeViewItem folderItem = new TreeViewItem
                {
                    Header = item.Name,
                    Tag = item.FullName
                };
                folderItem.Items.Add("*"); // Placeholder to indicate that it has subitems
                folderItem.Expanded += FolderItem_Expanded;
                treeView.Items.Add(folderItem); // 正确的添加到 treeView 中
            }
        }

        private void FolderItem_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem folderItem = (TreeViewItem)sender;
            if (folderItem.Items.Count == 1 && folderItem.Items[0] is string && (string)folderItem.Items[0] == "*")
            {
                folderItem.Items.Clear();
                string folderPath = (string)folderItem.Tag;
                try
                {
                    foreach (var subItem in Directory.GetDirectories(folderPath))
                    {
                        TreeViewItem subFolderItem = new TreeViewItem
                        {
                            Header = new DirectoryInfo(subItem).Name,
                            Tag = subItem
                        };
                        subFolderItem.Items.Add("*");
                        subFolderItem.Expanded += FolderItem_Expanded;
                        folderItem.Items.Add(subFolderItem);
                    }
                    foreach (var file in Directory.GetFiles(folderPath))
                    {
                        TreeViewItem fileItem = new TreeViewItem
                        {
                            Header = Path.GetFileName(file),
                            Tag = file
                        };
                        folderItem.Items.Add(fileItem);
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    // Handle unauthorized access (e.g., protected system folders)
                }
            }
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue is TreeViewItem selectedItem && selectedItem.Tag is string filePath)
            {
                if (File.Exists(filePath) && Path.GetExtension(filePath).Equals(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    // Handle opening .txt files
                    MessageBox.Show($"Opening {filePath}");
                }
            }
        }
    }
}
